package org.destiny.swing.mapping.base;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.lang.reflect.Field;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FileInputMapper extends AbstractFormItemMapper<File> {

	JPanel panel;

	public FileInputMapper(Field field) {
		super(field);
		panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		final JFileChooser fileChooser = new JFileChooser();
		final JTextField pathField = new JTextField(30);
		final JButton browserBtn = new JButton("浏览");
		browserBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int opt = fileChooser.showOpenDialog(SwingUtilities.getWindowAncestor(panel));
				if (opt == JFileChooser.APPROVE_OPTION) {
					pathField.setText(fileChooser.getSelectedFile().getAbsolutePath());
				}
			}
		});
		panel.add(pathField);
		panel.add(new JLabel(" "));
		panel.add(browserBtn);
	}

	@Override
	public JComponent getInputComponent() {
		return panel;
	}

	@Override
	public File getFieldValue() {
		return null;
	}

	@Override
	public void setFieldValue(File fieldValue) {
		
	}

}
